/**
 * 作业答题
 */
var localStorageManagers = require("script/localStorageManagers.js");
//mui初始化
mui.init({
	swipeBack: false
});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	//保存作业状态
	saveTaskStatus();
	//加载题目信息
	loadTimu();
	//切换题目
	mui('.top').on('tap', '.tihao1', clicktihao1); //已提交
	//左右滑动切换题目
	window.addEventListener("swipeleft", swipeLeft);
	window.addEventListener("swiperight", swipeRight);
	//提交作业
	mui('header').on('tap', '#tijiaotaskbtn', tijiaoTask);
	//保存作业
	mui('header').on('tap', '#savetaskbtn', saveTask);
	//点击附件
	mui('.mui-content').on('tap','.f1',openFile);
	//编辑答案
	mui('.footbtndiv').on('tap', '.footbtnspan', footbtnspanEvent);
};
//加载题目
function loadTimu() {
	ajaxLoadtimu();
}
//ajax加载题目
function ajaxLoadtimu(){
	var self=plus.webview.currentWebview();
	var taskid = self.taskid;
	var taskname=self.taskname;
	if(taskname.length>15)taskname=taskname.substring(0,15)+'...';
	document.getElementById('fTaskid').value = taskid;
	document.getElementById('fTaskname').value=taskname;
	document.getElementById('tasknameh1').innerHTML=taskname;
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + '/mobile/task/getTimusInfo.do';
	mui.ajax(url, {
		data: {
			taskid: taskid,
			userid: userid
		},
		type: 'post',
		dataType: 'json',
		timeout: 100000,
		success: function(data) {
			if (data == null || data.length == 0) return;
			buildTimuDivs(data);
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
}
//构建题目div
function buildTimuDivs(timus) {
	var timusEle = document.getElementById('elist1');

	for (var i = 0; i < timus.length; i++) {
		var timu = timus[i];
		//设置题号
		settihao(timu, i);
		//题目附件
		var filestr1 = '';
		for (var j = 0; j < timu.files.length; j++) {
			var path = timu.files[j].path;
			var name=timu.files[j].name;
			name=viewFilename(name);
			filestr1 += '<span class="f1" path="' + path + '">' + name + '</span>';
		}

		//题目
		var act = '';
		if (i == 0) act = 'act';
		var timustr = '<div class="task task1 ' + act + '" id="task1_' + timu.id + '">';
		timustr += '<div class="task-content">';
		timustr += '<div class="c1">' + timu.question + '</div>';
		timustr += '<div class="c2">';
		timustr += filestr1;
		timustr += '</div>';
		timustr += '</div>';
		timustr += '<div class="task-daan" id="task-daan_' + timu.id + '"></div>';
		timustr += '<div class="task-jiexi" id="task-jiexi_' + timu.id + '"></div>';
		timustr += '</div>';
		timusEle.innerHTML = timusEle.innerHTML + timustr;
		//题目答案
		setdaan(timu.daan);
		//批阅
		setpiyue(timu.daan);
	}
}
//设置题号
function settihao(timu, i) {
	var topEle = document.getElementById('top1');
	var tihaostr = '';
	var act = '';
	if (i == 0) {
		act = 'act';
		document.getElementById('fTihao').value = timu.id;
	}
	tihaostr = '<span class="tihao tihao1 ' + act + '" tihao="' + timu.id + '">' + (i + 1) + '</span>';
	topEle.innerHTML = topEle.innerHTML + tihaostr;
}
//设置答案
function setdaan(daan) {
	if (daan != undefined) {
		var filesstr = '';
		var pathstr='';
		var names='';
		for (var j = 0; j < daan.files.length; j++) {
			var path =  daan.files[j].path;
			var name=daan.files[j].name;
			name=viewFilename(name);
			filesstr += '<span class="f1" path="' + path + '">' + name+ '</span>';
			pathstr+=path+"|";
			names+=name+"|";
		}
		
		var daanEle = document.getElementById('task-daan_' + daan.extId);
		var daans = '<div class="t1">答案:</div>';
		daans += '<div class="t2" id="task-daan-content_' + daan.extId + '">' + daan.answer + '</div>';
		daans += '<div class="c2">';
		daans += filesstr;
		daans += '</div>';
		daanEle.innerHTML = daans;
	}
}
//设置批阅
function setpiyue(daan) {
	if (daan != undefined) {
		
		var daanEle = document.getElementById('task-jiexi_' + daan.extId);
		var jiexis = '<div class="e1">批阅作业:</div>';
		jiexis+= '<div class="e2">'+getScoreStr(daan.score)+'</div>';
		jiexis+= '<div class="e3">批阅意见</div>';
		jiexis+= '<div class="e4">'+daan.advice+'</div>';
		if(daan.score!=null&&daan.score!=0){
			daanEle.innerHTML = jiexis;
			daanEle.style.display='block';
		}
	}
}
//已提交的题目
function clicktihao1() {
	//题号按钮颜色改变
	var tihaos = document.getElementsByClassName('tihao1');
	for (var i = 0; i < tihaos.length; i++) {
		tihaos[i].className = 'tihao tihao1';
	}
	this.className = 'tihao tihao1 act';
	//显示对应题目
	var tasks = document.getElementsByClassName('task1');
	for (var i = 0; i < tasks.length; i++) {
		tasks[i].className = 'task task1';
	}
	var tihao = this.getAttribute('tihao');
	var timuDiv = document.getElementById('task1_' + tihao);
	timuDiv.className = 'task task1 act';
	document.getElementById('fTihao').value = tihao;
};
//提交作业
function tijiaoTask() {
	var istijiao = confirm('您确定要像老师提交本次作业吗?');
	if (!istijiao) return;
		var taskid = document.getElementById('fTaskid').value;
		var userid = localStorageManagers.getUseridSession();
		var url = serverAjax + '/mobile/task/submitOrCancelTask.do';
		mui.ajax(url, {
			data: {
				userid: userid,
				taskid: taskid,
				type: 1
			},
			type: 'post',
			dataType: 'json',
			timeout: 10000,
			success: function(data) {
				if (data == null || data.length == 0) return;
				if (data == 1) {
					mui.toast('提交作业成功');
					//this.src = '../../images/task_3.png';
					//this.className = 'canceltask';
				} else if (data == 0) {
					mui.toast('还未开始做作业');
				}
			},
			error: function(e1, e2, e3) {
				ajaxErrorTip();
				console.log(e1);
			}
		});
};
//保存作业
function saveTask() {
	mui.toast('已保存');
}
//编辑答案
function footbtnspanEvent() {
	var type = this.getAttribute("type");
	if (type == 1)
		editDaan(this);
	if (type == 2)
		editDaanFinish(this);

};
//点击编辑答案
function editDaan(ts) {
	ts.innerHTML = '完成';
	document.getElementById('daancontent').value = '';
	ts.setAttribute("type", "2");
	document.getElementById('editdiv').style.display = 'block';
	var tihao = document.getElementById('fTihao').value;
	toggleDaandisplay(tihao, 'none');
	//设置答案编辑框
	var extId = document.getElementById('fTihao').value;
	var daanContentEle = document.getElementById('task-daan-content_' + extId);
	if (daanContentEle != null && daanContentEle != undefined) document.getElementById('daancontent').value = daanContentEle.innerHTML;
};
//编辑完成
function editDaanFinish(ts) {
	ts.innerHTML = '编辑答案';
	ts.setAttribute("type", "1");
	document.getElementById('editdiv').style.display = 'none';
	var tihao = document.getElementById('fTihao').value;
	toggleDaandisplay(tihao, 'block');
	//设置编辑后答案
	var daancontent = document.getElementById('daancontent').value;
	var extId = document.getElementById('fTihao').value;
	var taskid = document.getElementById('fTaskid').value;
	var userid = localStorageManagers.getUseridSession();
	var daan = {
		answer: daancontent,
		extId: extId,
		paths: '',
		taskid: taskid,
		userid: userid
	}
	setdaan(daan);
	//保存答案
	saveDaan(daan);
}
//保存答案
function saveDaan(daan) {
	var url = serverAjax + '/mobile/task/saveStudentWork.do';
	mui.ajax(url, {
		data: daan,
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//查看附件
function openFile(){

	var path=this.getAttribute('path');
	path=fileServerAjax+path;
	plus.runtime.openURL(path, {}, function ( e ) {
			                  plus.nativeUI.alert( "无法打开'"+suffix+"'格式的文件,请安装相关应用." );
		             } );
};
//设置答案隐藏
function toggleDaandisplay(tihao, distype) {
	document.getElementById('task-daan_' + tihao).style.display = distype;
}
//保存作业状态
function saveTaskStatus() {
	var taskid = plus.webview.currentWebview().taskid;
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + '/mobile/task/saveWorkStatus.do';
	mui.ajax(url, {
		data: {
			taskid: taskid,
			userid: userid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {

		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//左滑切换题目
function swipeLeft(){
	var tihaos = document.getElementsByClassName('tihao1');
	var nextTihao=null;
	for (var i = 0; i < tihaos.length-1; i++) {
		if(tihaos[i].className.indexOf('act')!=-1)
		nextTihao=tihaos[i+1];
		tihaos[i].className = 'tihao tihao1';
	}
	if(nextTihao!=null){
	   nextTihao.className = 'tihao tihao1 act';
	   //显示对应题目
	var exams = document.getElementsByClassName('task1');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'task task1';
	}
	var tihao = nextTihao.getAttribute('tihao');
	var timuDiv = document.getElementById('task1_' + tihao);
	timuDiv.className = 'task task1 act';
	}
};
//右滑切换题目
function swipeRight(){
	var tihaos = document.getElementsByClassName('tihao1');
	var nextTihao=null;
	for (var i = 1; i < tihaos.length; i++) {
		if(tihaos[i].className.indexOf('act')!=-1)
		nextTihao=tihaos[i-1];
		tihaos[i].className = 'tihao tihao1';
	}
	if(nextTihao!=null){
	    nextTihao.className = 'tihao tihao1 act';
	    //显示对应题目
	var exams = document.getElementsByClassName('task1');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'task task1';
	}
	var tihao = nextTihao.getAttribute('tihao');
	var timuDiv = document.getElementById('task1_' + tihao);
	timuDiv.className = 'task task1 act';
	}
};
//批阅结果
function getScoreStr(score){
	var s='';
	switch(score){
	case -1:
	s='正确';break;
	case -2:
	s='错误';break;
	case -3:
    s='合格';break;
	case -4:
	s='不合格';break;
	case -5:
	s='优秀';break;
	case -6:
	s='良好';break;
	case -7:
	s='一般';break;
	case -8:
	s='较差';break;
	case -9:
	s='已批阅';break;
	case -10:
	s='打回修改';break;
	default:
	s='已批阅';
	}
	return s;
}
function viewFilename(name){
	if(name.length>6)name=name.substring(0,6)+'...';
	return name;
}